$(function () {
    var web = {
        parameter: {
            swich_1: true,
            swich_2: true,
            swich_3: true,
            swich_4: true,
            swich_5: true,
            swich_6: true,
            swich_7: true
        },
        main: function () {
            web.appAnimate();
            web.loadImg();
        },
        loadImg: function () {
            var len = $("img[name='load']").length,
                scrollTop = 0,
                swich = true,
                setTimes = "";

            $(window).ready(function () {
                scrollTop = $(document).scrollTop();

                if (scrollTop > 100 && swich) loads();
            });

            $(window).scroll(function () {
                scrollTop = $(document).scrollTop();

                if (scrollTop > 100 && swich) loads();
            });

            setTimes = setTimeout(function () {
                if (swich) loads();
            }, 2000)

            function loads() {
                for (var i = 0; i < len; i++) {
                    $("img[name='load']").eq(i).attr("src", $("img[name='load']").eq(i).data("src"));
                }

                swich = false;
            }
        },
        appAnimate: function () {
            var $appCon_1 = $(".des-one"),
                $appImg_1 = $(".img-one"),
                $appCon_2 = $(".des-two"),
                $appImg_2 = $(".img-two"),
                $appCon_3 = $(".des-three"),
                $appImg_3 = $(".img-three"),
                $appCon_4 = $(".des-four"),
                $appImg_4 = $(".img-four"),
                $appCon_5 = $(".des-five"),
                $appImg_5 = $(".img-five"),
                $appCon_6 = $(".des-six"),
                $appImg_6 = $(".img-six"),
                $appCon_7 = $(".des-seven"),
                $appImg_7 = $(".img-seven"),
                width = 0,
                height = 0,
                scrollTop = 0,
                top = 0;

            function res_1() {
                $appImg_1.add($appCon_1).css({
                    "transform": "translate(0,0)",
                    "opacity": 1
                });
                web.parameter.swich_1 = false
            }

            function comRes($con, $img, swich, mtop) {
                top = $con.offset().top;
                scrollTop = $(document).scrollTop();
                height = $(window).height();

                if (top - height + 320 < scrollTop) {
                    $con.css({
                        "transform": "translate(0,0)",
                        "opacity": 1
                    });
                    $img.css({
                        "transform": "translate(0,0)",
                        "opacity": 1
                    });
                    swich = false
                }
            }

            $(window).scroll(function () {
                if (web.parameter.swich_2) comRes($appCon_2, $appImg_2, web.parameter.swich_2);
                if (web.parameter.swich_3) comRes($appCon_3, $appImg_3, web.parameter.swich_3);
                if (web.parameter.swich_4) comRes($appCon_4, $appImg_4, web.parameter.swich_4);
                if (web.parameter.swich_5) comRes($appCon_5, $appImg_5, web.parameter.swich_5);
                if (web.parameter.swich_6) comRes($appCon_6, $appImg_6, web.parameter.swich_6);
                if (web.parameter.swich_7) comRes($appCon_7, $appImg_7, web.parameter.swich_7);
                if (web.parameter.swich_8) comRes($appCon_8, $appImg_8, web.parameter.swich_8);
            });

            res_1();
        }
    };


    var winHeight = typeof window.innerHeight != 'undefined' ? window.innerHeight : document.documentElement.clientHeight; //兼容IOS，不需要的可以去掉

    function getAndroidApp() {
        $.ajax({
            url: "/clmp/appVersion/post_checkUpdate",
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.status === 1) {
                    console.log(data);
                    var versions = data.versions;
                    if (versions && versions.length > 0) {
                        $(".app_version_android").text(versions[0].ver_name);
                        $(".btn-android").attr("href", versions[0].apk_path);
                    }
                }
            }
        });
    }

    function is_weixn() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        } else {
            return false;
        }
    }

    function openInDefault() {

        var tip = document.getElementById('weixin-tip');
        var close = document.getElementById('close');

        tip.style.height = winHeight + 'px'; //兼容IOS弹窗整屏
        tip.style.display = 'block';

        close.onclick = function () {
            tip.style.display = 'none';
        }
    }

    function browserRedirect() {
        $(".body").css("height", winHeight);
        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

        //var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1 ;
        if (bIsIpad || bIsIphoneOs) {

            if (is_weixn()) {
                $(".btn-apple").hide();
                $(".btn-android").hide();
                openInDefault();
            }else{
                $(".btn-apple").show();
                $(".btn-android").hide();
            }
        } else if (bIsAndroid) {
            $(".btn-android").show();
            $(".btn-apple").hide();
            if (is_weixn()) {
                $(".btn-apple").hide();
                $(".btn-android").hide();
                openInDefault();
            } else {
                getAndroidApp();
            }
        } else if (bIsUc7 || bIsUc) {
            openInDefault();
        }
        getAndroidApp();
    }
    
    function openModal() {
        var left = ($(window).width() - $('#phoneBox').width()) / 2;
        $.blockUI({
            message: $('#phoneBox'),
            onOverlayClick: $.unblockUI,
            css: {
                top: '90px',
                width: 'auto',
                border: '0',
                position: 'absolute',
                left: left,
                width: '90%',
                maxWidth: '860px',
                margin: '0 auto'
            }
        });
    }
    
    $(window).resize(function(){
        var left = ($(window).width() - $('#phoneBox').width()) / 2;
        if($(window).width() < 1000){
            left = left / 2;
        }
        $('.blockMsg').css('left', left);
    });
    
    $('.mobile-list, .mobile-list1').click(function(){
        openModal();
        $('#phoneClose').show();
    });
    
    $('#phoneClose').click(function(e){
        e.preventDefault();
        $.unblockUI();
        $('#phoneClose').hide();
    });
    
    
    web.main();
    browserRedirect();
});